import Map = require('esri/Map');
import QueryTask = require('esri/tasks/QueryTask');
import Query = require('esri/tasks/support/Query');
import MapView = require('esri/views/MapView');

import Config from './../../js/modules/Config.js';

class layerList {
    private map: Map;
    private container: string;
    private mapView: MapView;
    /**
     * 初始化图层列表小部件
     * @param map 地图对象
     * @param mapView 二维视图对象
     * @param container 用于放置小部件的容器（选择器）
     */
    constructor(map: Map, mapView: MapView, container: string) {
        this.map = map;
        this.mapView = mapView;
        this.container = container;
    }
    /**
     * 启动小部件
     */
    startup() {
        this.InitDom();
        this.InitEvent();
    }
    private InitDom() {
        $(this.container).load("/js/widgets/layerList/layerList.html");
    }
    private InitEvent() {
        let _self = this;
        //zTree
        let zTreeObj_map: any;

        //图层控制
        function zTreeOnCheck_map() {
            let checkedNodes = zTreeObj_map.getCheckedNodes(true);
            let visibleLyr: number[] = [];
            $.each(checkedNodes, function (i, n) {
                switch (n.name) {
                    case "车站":
                        visibleLyr.push(3);
                        break;
                    case "区间":
                        visibleLyr.push(2);
                        break;
                    case "保护区":
                        visibleLyr.push(1);
                        break;
                    case "地形图":
                        visibleLyr.push(0);
                        break;
                }
            });
            $.each(_self.map.layers.items, function (i, n) {
                n.visible = false;
            });
            $.each(visibleLyr, function (i, n) {
                _self.map.layers.items[n].visible = true;
            })
        }

        //要素定位
        const query = new Query({
            returnGeometry: true,
            outFields: ["refname"]
        })
        const queryTask = new QueryTask();

        function zTreeOnClick_map(event: any, treeId: any, treeNode: any, clickFlag: any) {
            let findUrl = "";
            let parentNode = treeNode.getParentNode();
            switch (parentNode.name) {
                case "车站":
                    findUrl = Config.mapConfig.mapLyr_chezhan + "/0";
                    break;
                case "区间":
                    findUrl = Config.mapConfig.mapLyr_qujian + "/0";
                    break;
            }
            query.where = `refname='${treeNode.name}'`
            queryTask.url = findUrl;
            queryTask.execute(query).then(function (results) {
                _self.mapView.goTo(results.features[0].geometry);
            })
        }
        const setting_map = {
            check: {
                enable: true
            },
            callback: {
                onCheck: zTreeOnCheck_map,
                onClick: zTreeOnClick_map
            }
        };
        $.getJSON("/js/widgets/layerList/zNodes.json", function (zNodesResults: any) {
            const zNodes_map = zNodesResults.map;
            zTreeObj_map = $.fn.zTree.init($("#navTree_map"), setting_map, zNodes_map);
        })
    }
}
export default layerList;